<template>
  <el-container class="reset-container">
    <!-- 左侧品牌信息 -->
    <el-col :span="12" class="left-panel">
      <div class="brand-wrapper">
        <h1 class="brand-title">家政助手</h1>
        <p class="brand-subtitle">专业 · 可靠 · 让生活更轻松</p>
        <img src="../assets/images/cleaner.png" alt="家政服务" class="brand-image" />
      </div>
    </el-col>

    <!-- 忘记密码区域 -->
    <el-col :span="12" class="right-panel">
      <el-card class="reset-card" shadow="always">
        <h2 class="reset-title">重置密码</h2>
        <el-form :model="form" label-position="top" @submit.prevent="handleReset">
          <el-form-item label="手机号">
            <el-input v-model="form.phone" placeholder="请输入注册手机号" clearable prefix-icon="Phone" />
          </el-form-item>
          <el-form-item label="验证码">
            <el-input v-model="form.code" placeholder="请输入验证码" clearable prefix-icon="Edit" />
            <el-button class="get-code-btn" type="primary" plain :disabled="isCodeDisabled"
              @click="getCode">{{ codeText }}</el-button>
          </el-form-item>
          <el-form-item label="新密码">
            <el-input
              v-model="form.newPassword"
              type="password"
              placeholder="请输入新密码"
              show-password
              clearable
              prefix-icon="Lock"
            />
          </el-form-item>
          <el-form-item label="确认密码">
            <el-input
              v-model="form.confirmPassword"
              type="password"
              placeholder="请再次输入新密码"
              show-password
              clearable
              prefix-icon="Key"
            />
          </el-form-item>
          <el-button type="primary" native-type="submit" class="reset-btn" block>重置密码</el-button>
        </el-form>
        <div class="extra-links">
          <a href="/">返回登录页</a>
        </div>
      </el-card>
    </el-col>
  </el-container>
</template>

<script setup>
import { ref } from 'vue'
import { useRouter } from 'vue-router'
import axios from 'axios'
import { ElMessage } from 'element-plus'
// 验证码相关状态
const codeText = ref('获取验证码')
const isCodeDisabled = ref(false)
const defaultCode = '12345' // 默认验证码
let countdown = 60
const router = useRouter()
const form = ref({
  phone: '',
  code: '',
  newPassword: '',
  confirmPassword: ''
})

const handleReset = async () => {
  const { phone, code, newPassword, confirmPassword } = form.value

  if (!phone || !code || !newPassword || !confirmPassword) {
    ElMessage.error('请填写所有字段')
    return
  }

  if (newPassword !== confirmPassword) {
    ElMessage.error('两次密码不一致')
    return
  }

  // 模拟验证码校验
  if (code !== '12345') {
    ElMessage.error('验证码错误')
    return
  }

  try {
    // Step 1: 根据手机号查询用户
    const response = await axios.get(`http://localhost:3000/users?phone=${phone}`)

    if (response.data.length === 0) {
      ElMessage.error('该手机号未注册')
      return
    }

    const user = response.data[0]

    // Step 2: 更新用户密码
    await axios.patch(`http://localhost:3000/users/${user.id}`, {
      password: newPassword
    })

    ElMessage.success('密码重置成功！即将跳转至登录页...')
    setTimeout(() => {
      router.push('/login')
    }, 1000)
  } catch (error) {
    console.error('密码重置失败:', error)
    ElMessage.error('网络异常，请稍后再试')
  }
}
const getCode = () => {
  if (isCodeDisabled.value) return

  // 将验证码赋值为默认值
  form.value.code = defaultCode

  isCodeDisabled.value = true
  codeText.value = `${countdown}s`

  const timer = setInterval(() => {
    countdown--
    codeText.value = `${countdown}s`

    if (countdown <= 0) {
      clearInterval(timer)
      codeText.value = '获取验证码'
      isCodeDisabled.value = false
      countdown = 60
    }
  }, 1000)
}
</script>

<style scoped>
.reset-container {
  height: 100vh;
  font-family: 'Segoe UI', sans-serif;
  background: linear-gradient(135deg, #f8f9fa, #eef2f3);
}

.left-panel {
  background: linear-gradient(to bottom right, #4e54c8, #8f94fb);
  color: white;
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 60px;
}

.brand-wrapper {
  text-align: center;
  max-width: 400px;
}

.brand-title {
  font-size: 36px;
  margin-bottom: 10px;
  font-weight: bold;
}

.brand-subtitle {
  font-size: 18px;
  opacity: 0.9;
  margin-bottom: 30px;
}

.brand-image {
  width: 220px;
  max-width: 100%;
  border-radius: 12px;
  box-shadow: 0 6px 16px rgba(0, 0, 0, 0.2);
}

.right-panel {
  background-color: #f9fafb;
  display: flex;
  align-items: center;
  justify-content: center;
}

.reset-card {
  width: 420px;
  padding: 40px;
  border-radius: 16px;
  box-shadow: 0 12px 32px rgba(0, 0, 0, 0.1);
  background-color: white;
  transition: all 0.3s ease-in-out;
}

.reset-card:hover {
  transform: translateY(-5px);
}

.reset-title {
  text-align: center;
  margin-bottom: 30px;
  font-size: 26px;
  color: #333;
  font-weight: bold;
}

/* 表单项样式 */
:deep(.reset-card .el-form-item__label) {
  font-weight: 600;
  color: #333;
  padding-bottom: 8px;
  font-size: 14px;
}

:deep(.reset-card .el-input__inner) {
  padding-left: 40px;
  border-radius: 8px;
  border: 1px solid #dcdfe6;
  height: 48px;
  font-size: 15px;
  transition: all 0.3s ease-in-out;
}

:deep(.reset-card .el-input__prefix) {
  left: 12px;
  font-size: 18px;
  color: #999;
}

/* 验证码按钮 */
.get-code-btn {
  width: 100%;
  margin-top: 8px;
  height: 40px;
  background-color: #0c3b99;
  color: white;
  font-size: 14px;
  transition: all 0.3s ease-in-out;
}

.get-code-btn:hover {
  background-color: #092e78;
  opacity: 0.9;
}

/* 提交按钮 */
.reset-btn {
  width: 100%;
  height: 48px;
  font-size: 16px;
  font-weight: bold;
  letter-spacing: 0.5px;
  background: linear-gradient(to right, #4e54c8, #8f94fb);
  border: none;
  border-radius: 8px;
  color: white;
  transition: all 0.3s ease-in-out;
}

.reset-btn:hover {
  opacity: 0.9;
  transform: translateY(-2px);
  box-shadow: 0 6px 16px rgba(78, 84, 200, 0.3);
}

.extra-links {
  display: flex;
  justify-content: center;
  margin-top: 20px;
  font-size: 14px;
}

.extra-links a {
  color: #409EFF;
  text-decoration: none;
  transition: color 0.2s ease;
}

.extra-links a:hover {
  color: #337ecc;
  text-decoration: underline;
}
</style>